package app.server.project.core.http;

import app.server.project.core.exception.GlobalException;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class RespModel<T> {
    private int code = 200;
    private T data;
    private String msg;

    public static RespModel error(int code) {
        return new RespModel().setCode(code);
    }

    public static RespModel error(int code, String msg) {
        return new RespModel()
                .setMsg(msg)
                .setCode(code);
    }

    public static RespModel success(Object data) {
        return new RespModel()
                .setData(data);
    }

    public static RespModel success() {
        return new RespModel();
    }

    public static RespModel error(GlobalException exception) {
        var ret = new RespModel();
        BeanUtil.copyProperties(exception, ret);
        return ret;
    }

    @JsonIgnore
    public boolean isSuccess() {
        return code == 200;
    }

    @JsonIgnore
    public boolean isFailure() {
        return code != 200;
    }

    @JsonIgnore
    public String toStr() {
        return JSONUtil.toJsonStr(this);
    }

}
